package trans.model;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import trans.model.base.BaseSa;
import trans.service.UserDAO;
import trans.support.Constants;



public abstract class Sa extends BaseSa {
	public static final long serialVersionUID = 1L;
	
	private Set<SaStateHis> stateHisSet = new LinkedHashSet<SaStateHis>();
	
	public Set<SaStateHis> getStateHisSet() {
		for(SaStateHis each : this.getStateHises()){
			stateHisSet.add(each);
		}
		return stateHisSet;
	}

	public abstract String getStateShow();
	
	public abstract boolean isFreeze(User invorkUser);
	
	public abstract void initStates(User user,Map<Integer,String> usableStates);
	
	public abstract void prepareSaStateShow(HttpServletRequest request);
	
	public abstract void prepareTransNoShow(HttpServletRequest request) ;
	
	public abstract String getViewName() ;
	
	public abstract String getEditName() ;
	
	public void saveTips(Integer member_mawbShipper,Integer member_mawbConsignee,
			Integer member_hawbShipper,Integer member_hawbConsignee){
		try {
			if(member_mawbShipper != null && member_mawbShipper == 1){
				UserDAO.saveTip(this.getClientId(), Constants.USERTIP_MAWBSHIPPER, this.getMawbShipper());
			}
			if(member_mawbConsignee != null &&member_mawbConsignee == 1){
				UserDAO.saveTip(this.getClientId(), Constants.USERTIP_MAWBCONSIGNEE, this.getMawbConsignee());
			}
			if(member_hawbShipper!=null && member_hawbShipper == 1){
				UserDAO.saveTip(this.getClientId(), Constants.USERTIP_HAWBSHIPPER, this.getHawbShipper());
			}
			if(member_hawbConsignee!= null && member_hawbConsignee == 1){
				UserDAO.saveTip(this.getClientId(), Constants.USERTIP_HAWBCONSIGNEE, this.getHawbConsignee());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private Integer saOlderState;
	
	public Integer getSaOlderState() {
		return saOlderState;
	}

	public void setSaOlderState(Integer saOlderState) {
		this.saOlderState = saOlderState;
	}
	
	private String stateChangeTime;
	
	public String getStateChangeTime() {
		return stateChangeTime;
	}

	public void setStateChangeTime(String stateChangeTime) {
		this.stateChangeTime = stateChangeTime;
	}
	
	public  abstract boolean caculatePendding();
	
	private String newRemark;

	public String getNewRemark() {
		return newRemark;
	}

	public void setNewRemark(String newRemark) {
		this.newRemark = newRemark;
	}
	
	public void addNewRemark2Remark(String executor,List<User> sysusers,User currentUser){
		if(StringUtils.isEmpty(this.newRemark) == false){
			String rawRemark = this.getRemark();
			if(rawRemark == null){
				rawRemark = "";
			}
			this.setRemark(rawRemark + executor + "[" +DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm")+"]:"+newRemark+"\n");
		
			reflush(sysusers,currentUser);
		}
	}

	private void reflush(List<User> sysusers,User currentUser) {
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for(User eachSysuser : sysusers){
			if(eachSysuser.getId().equals(currentUser.getId())){
				continue;
			}
			if(i!=0){
				sb.append("," + eachSysuser.getId());
			}else{
				sb.append(eachSysuser.getId());
			}
			i++;
		}
		this.setHaveMsgUser(sb.toString());
	}
	
	public void updateMsgUser(User currentUser){
		if(StringUtils.isEmpty(this.getHaveMsgUser()) == true){
			return;
		}
		
		StringBuffer sb = new StringBuffer();
		String[] idArr = this.getHaveMsgUser().split(",");
		int i = 0;
		for(String eachId : idArr){
			if(eachId.equals(currentUser.getId() + "")){
				continue;
			}
			if(i!=0){
				sb.append("," + eachId);
			}else{
				sb.append(eachId);
			}
			i++;
		}
		this.setHaveMsgUser(sb.toString());
	}
	
	private boolean haveNewMsg;

	public boolean isHaveNewMsg() {
		return haveNewMsg;
	}

	public void setHaveNewMsg(boolean haveNewMsg) {
		this.haveNewMsg = haveNewMsg;
	}
	

	/*[CONSTRUCTOR MARKER BEGIN]*/
	public Sa () {
		super();
	}

	/**
	 * Constructor for primary key
	 */
	public Sa (java.lang.Integer id) {
		super(id);
	}

/*[CONSTRUCTOR MARKER END]*/


}